package com.act;

import java.util.ArrayList;
import java.util.Date;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bean.Collect;
import com.bean.CollectList;
import com.bean.UserInfo;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.easemob.util.DateUtils;
import com.mUI.XListView;
import com.mUI.XListView.IXListViewListener;
import com.xike.imageloader.core.ImageLoader;
import com.xike.kyt.R;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class Mine_CollectsAct extends BaseAct {
	TextView tv_back;
	XListView listView;
	MyAdapter adapter;
	int pageCursor = 1;
	boolean isRefresh = false;
	boolean isLoadingMore = false;
	CollectList collectList;
	ArrayList<Collect> collects = new ArrayList<Collect>();

	BitmapDrawable defualtBitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_my_collects);
		initViews();
		serviceGetMyCollects();
	}

	@Override
	protected void initViews() {
		defualtBitmap = (BitmapDrawable) getResources().getDrawable(R.drawable.default_user_avator);

		tv_back = (TextView) findViewById(R.id.headTitle_tv_back);
		tv_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		listView = (XListView) findViewById(R.id.myCollects_listView);
		listView.setPullRefreshEnable(true);
		listView.setPullLoadEnable(true);
		listView.setAutoLoadEnable(true);

		listView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pageCursor = 1;
				isRefresh = true;
				serviceGetMyCollects();
			}

			@Override
			public void onLoadMore() {
				pageCursor++;
				isLoadingMore = true;
				serviceGetMyCollects();
			}
		});

		adapter = new MyAdapter();
		listView.setAdapter(adapter);

	}

	@Override
	protected void updateViews() {
		ArrayList<Collect> loadCollects = collectList.getCollects();
		if (loadCollects == null || loadCollects.size() == 0) {
			if (pageCursor == 1) {
				findViewById(R.id.layout_nodata).setVisibility(View.VISIBLE);
				listView.setVisibility(View.GONE);
				((ImageView) findViewById(R.id.layout_nodata_img)).setImageResource(R.drawable.default_nodata_collect);
				((TextView) findViewById(R.id.layout_nodata_tv_tips)).setText("没有任何收藏\n可以收藏您所感兴趣的回答");
			}
		} else {
			findViewById(R.id.layout_nodata).setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);

			if (isRefresh) {
				collects.clear(); // 刷新 清空
				collects.addAll(loadCollects);
				adapter.notifyDataSetChanged();
				listView.stopRefresh();
				listView.updateRefreshTime();
				isRefresh = false;
				listView.setPullLoadEnable(true);
			} else {
				collects.addAll(loadCollects);
				adapter.notifyDataSetChanged();
				if (isLoadingMore) {
					listView.stopLoadMore();
				}
				if (collectList.getNext_cursor() > 0) {
					listView.showFooterView();
				} else {
					listView.setPullLoadEnable(false);
				}
			}
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Collect collect = adapter.getItem(position - 1);
					int infoType = collect.getInfo_type();
					Intent it = null;
					if (infoType == 10) {
						it = new Intent(Mine_CollectsAct.this, Answer_DetailAct.class);
						it.putExtra("answerId", collect.getInfo_id());
						startActivity(it);
					}

					// Intent it_questionDetail = new Intent(MyCollectsAct.this, QuestionDetailAct.class);
					// it_questionDetail.putExtra("questionId", collect.getId());
					// startActivity(it_questionDetail);
				}

			});
		}
	}

	private void serviceGetMyCollects() {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("page_cursor", String.valueOf(pageCursor)));
		params.add(new BasicNameValuePair("page_count", String.valueOf(Constant.LIST_PAGESIZE)));

		String strUrl = MyUtil.signRequestUrl(Constant.SERVER_URL_MIME_COLLECTS, params);
		client.get(this, strUrl, new TextHttpResponseHandler(this) {

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				mToast.showToast(Mine_CollectsAct.this, getString(R.string.net_error));
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				super.onSuccess(statusCode, headers, responseString);
				CollectList result = gson.fromJson(responseString, CollectList.class);
				dealRequestResult(result);
			}

		});
	}

	private void dealRequestResult(CollectList result) {
		if (result != null) {
			if (result.isReturnTrue()) {
				collectList = result;
				updateViews();
			} else {
				mToast.showToast(Mine_CollectsAct.this, result.getError_description());
			}
		} else {
			mToast.showToast(Mine_CollectsAct.this, getString(R.string.net_error));
		}
	}

	class MyAdapter extends BaseAdapter {
		class ViewHolder {
			ImageView img_userAvator;
			TextView tv_name;
			ImageView img_content;
			TextView tv_title, tv_createTime, tv_summary;
		}

		@Override
		public int getCount() {
			return collects.size();
		}

		@Override
		public Collect getItem(int position) {
			return collects.get(position);
		}

		@Override
		public long getItemId(int position) {
			return collects.get(position).getId();
		}

		ViewHolder holder;

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(Mine_CollectsAct.this, R.layout.listitem_my_collect, null);
				holder = new ViewHolder();
				holder.img_content = (ImageView) convertView.findViewById(R.id.lvitem_myCollect_img_content);
				holder.img_userAvator = (ImageView) convertView.findViewById(R.id.lvitem_myCollect_img_avator);

				holder.tv_name = (TextView) convertView.findViewById(R.id.lvItem_myCollect_tv_name);
				holder.tv_title = (TextView) convertView.findViewById(R.id.lvItem_myCollect_tv_title);
				holder.tv_summary = (TextView) convertView.findViewById(R.id.lvItem_myCollect_tv_summary);
				holder.tv_createTime = (TextView) convertView.findViewById(R.id.lvItem_myCollect_tv_time);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Collect collect = getItem(position);
			UserInfo user = collect.getUser();
			if (user != null) {
				MyUtil.setTextViewText(holder.tv_name, user.getShow_name(), View.GONE, "");
				MyUtil.displayImageView(Mine_CollectsAct.this, user.getAvator(), holder.img_userAvator, MyUtil.getRoundedCornerBitmap(defualtBitmap.getBitmap()), true);
			}

			MyUtil.setTextViewText(holder.tv_createTime, DateUtils.getTimestampString(new Date(collect.getCreate_time())), View.GONE, "");
			MyUtil.setTextViewText(holder.tv_title, collect.getTitle(), View.GONE, "");
			MyUtil.setTextViewText(holder.tv_summary, collect.getSummary(), View.GONE, "");

			if (StringUtil.isNotNull(collect.getImage())) {
				holder.img_content.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(collect.getImage(), holder.img_content);
			} else {
				holder.img_content.setVisibility(View.GONE);
			}

			return convertView;
		}
	}

}
